"""
Case Type   : 功能测试
Case Name   : 基于mot表创建增量物化视图，合理报错
Create At   : 2022/5/10
Owner       : @zou_jialiang0501162244
Description :
    1.设置enable_incremental_checkpoint参数为off,重启数据库
    2.创建mot表，插入数据
    3.基于mot表创建增量物化视图
    4.清理环境
Expect      :
    1.设置成功，重启数据库成功
    2.创建成功，插入数据成功
    3.创建增量物化视图失败,合理报错
    4.成功
History     :
    modified by nanyang12 2025/3/18: 研发代码变更，增加motserver开关配置
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Incrementalmatview(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.commonsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table_name = 't_imv_0018'
        self.im_view = 'v_imv_0018'

    def test_create_imv_on_mot(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step = '----step1:修改参数并重启数据库 expect:成功----'
        self.log.info(step)
        res = self.commonsh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_mot_server=on')
        self.assertTrue(res)
        self.config_item = "enable_incremental_checkpoint"
        self.param = self.common.show_param(self.config_item)
        if 'off' not in self.param:
            self.commonsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = off')
        self.commonsh.restart_db_cluster()
        result = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in result or "Normal" in result)
        current_value = self.common.show_param(self.config_item)
        self.assertTrue("off" in current_value, '修改失败')
        step = "----step2:创建mot表并插入数据 expect:成功----"
        self.log.info(step)
        create_mot = f"drop foreign table " \
                     f"if exists {self.table_name} cascade;"\
                     f"create foreign table {self.table_name}" \
                     f"(id int,name varchar(10));"\
                     f"insert into {self.table_name} values (1,'abc');"
        msg = self.commonsh.execut_db_sql(create_mot)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg,
                      f'执行create失败:{step}')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg,
                      f'执行insert失败:{step}')
        step = "----step3:基于mot表创建增量物化视图 expect:合理报错----"
        self.log.info(step)
        create_imv = f"create incremental materialized view " \
                     f"{self.im_view} as select * from {self.table_name};"
        msg = self.commonsh.execut_db_sql(create_imv)
        self.log.info(msg)
        err_text = 'ERROR:  relation "t_imv_0018" is not a regular table'
        self.assertIn(err_text, msg, f'执行失败:{step}')

    def tearDown(self):
        step = '----step4:清理环境 expect:成功----'
        self.log.info(step)
        drop_mot = (f'''drop foreign table {self.table_name} cascade;
                        CHECKPOINT;''')
        msg = self.commonsh.execut_db_sql(drop_mot)
        self.log.info(msg)
        res = self.commonsh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_mot_server=off')
        self.assertTrue(res)
        self.commonsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                    f'{self.config_item} = {self.param}')
        self.commonsh.restart_db_cluster()
        result = self.commonsh.get_db_cluster_status()
        current_value = self.common.show_param(self.config_item)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg,
                      f'执行失败:{step}')
        self.assertTrue("Degraded" in result or "Normal" in result,
                        f'执行失败:{step}')
        self.assertTrue(self.param in current_value, f'执行失败:{step}')
        self.log.info(f'-----{os.path.basename(__file__)} end------')
